package com.zb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.config.TokenDecode;
import com.zb.dto.AddressDto;
import com.zb.entity.TbAddressModel;
import com.zb.service.TbAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName TbAddressController
 * @Description 控制器
 * @Author zj
 * @Date 2022/11/09 15:46
 **/
@RestController
@RequestMapping("/tb-address-model")
public class TbAddressController {

    @Autowired
    public TbAddressService tbAddressModelService;

    @GetMapping("/myaddr")
    public List<AddressDto> myAddress() {
        String username = TokenDecode.getUserInfo().get("username");
        QueryWrapper<TbAddressModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbAddressModel::getUsername, username);
        List<AddressDto> addressDtoList = tbAddressModelService.list(queryWrapper).stream().map(item -> {
            AddressDto addressDto = new AddressDto();
            BeanUtils.copyProperties(item, addressDto);
            return addressDto;
        }).collect(Collectors.toList());
        return addressDtoList;
    }

}
